package rs.etf.mips.crossroad.logic;

public class PedestrianSemaphore {

	public enum State {
		Waiting, Crossing
	}

	private static final long yellow = 8000; // ms
	private static final long blink = 700;

	private long timeLeft;
	private State state;

	public PedestrianSemaphore(long timeLeft) {
		super();
		this.timeLeft = timeLeft;
		this.state = State.Waiting;
	}

	public PedestrianSemaphore() {
		this(0);
	}

	public long getTimeLeft() {
		return timeLeft;
	}

	public void setTimeLeft(long timeLeft) {
		this.timeLeft = timeLeft;
	}

	public void setState(State state){
		this.state = state;
	}
	
	public void tick(long time){
		this.timeLeft -= time;
		if (this.timeLeft<0) {
			this.timeLeft = 0;
		}
	}
	
	public Light getSemaforeLight() {
		Light light = this.state == State.Waiting ? Light.Red : Light.Green;
		if (this.timeLeft <= yellow && this.state==State.Crossing) {
			long blinkNum = this.timeLeft / blink;
			light = blinkNum % 2 == 1 ? Light.Off : Light.Red;
		}

		return light;
	}

	public Light getDisplayLight() {
		Light light = this.state == State.Waiting ? Light.Red : Light.Green;
		if (this.timeLeft <= yellow && this.state==State.Crossing)
			light = Light.Yellow;
		return light;
	}
}
